package ui.fragment;

import android.Manifest;
import android.annotation.SuppressLint;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.location.LocationManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.dispatchapp.andlei.R;
import com.mylhyl.acp.Acp;
import com.mylhyl.acp.AcpListener;
import com.mylhyl.acp.AcpOptions;
import com.zyao89.view.zloading.ZLoadingDialog;
import com.zyao89.view.zloading.Z_TYPE;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import adapter.OrderListAdapter;
import base.BaseBean;
import base.fragment.BaseFragment;
import bean.NoticeConfigBean;
import bean.OrderListBean;
import bean.OrderListMoreBean;
import netWork.NetUtils;
import ui.activity.homepage.DispatchRiderActivity;
import ui.activity.homepage.OrderDetailActivity;
import utils.GsonUtils;
import utils.Logger;
import utils.SPUtils;
import widget.ClearEditText;
import widget.HiDialog;
import widget.SieveHomePagePopupWindow;
import zxing.activity.CaptureActivity;

/**
 * 首页頁面
 *
 * @author Andlei
 */
public class HomepageFragment extends BaseFragment {
    private SwipeRefreshLayout swipeRefreshLayout;
    private RecyclerView recyclerView;
    private OrderListAdapter adapter;
    private RadioGroup radioGroup;
    private RadioButton rb0, rb1, rb2, rb3, rb4, rb44, rb5;
    private List<OrderListBean> list = new ArrayList<>();
    private int page = 0;
    private ImageView img_scanner;
    private ClearEditText editText;
    private TextView tv_shaixuan;
    private RelativeLayout rlayout_xiugai;
    private TextView tv_jiedan;
    private Button btn_suresend;
    private NetUtils netUtils;
    private View mView;
    private SieveHomePagePopupWindow pop;
    private String status = "received_order";
    private String addr_id = "";
    private String type_id = "";
    private String can_id = "";
    private ZLoadingDialog dialog;
    private CheckBox checkbox_all;
    private int mposition;
    private int Msg_type = 0; //区域配置 是否必要发短信
    private int send_msg = 0; // 选择是否发短信 0 不发，1 发

    private LinearLayout layout_btn;
    private TextView tv_left, tv_right;

    //弹幕控件
//    private boolean showDanmaku;
//    private DanmakuView danmakuView;
//    private DanmakuContext danmakuContext;
//    private BaseDanmakuParser parser = new BaseDanmakuParser() {
//        @Override
//        protected IDanmakus parse() {
//            return new Danmakus();
//        }
//    };

    public static HomepageFragment getInstance() {
        return new HomepageFragment();
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (hidden) {
            //  toast("隐藏了");
        } else {
            addr_id = "";
            if (MineFragment.ischangeIdentity == true) {
                pop = null;
                MineFragment.ischangeIdentity = false;
            }
            page = 0;
            loaddata();
            //1校内骑手  2调度员 3校外骑手 0未指定身份
            if (SPUtils.getInstance(mActivity).getString("Is_coach").equals("1")) {
                rb0.setVisibility(View.GONE);
                rb4.setVisibility(View.VISIBLE);
                rb5.setVisibility(View.VISIBLE);
            } else if (SPUtils.getInstance(mActivity).getString("Is_coach").equals("3")) {
                rb0.setVisibility(View.VISIBLE);
                rb4.setVisibility(View.GONE);
                rb5.setVisibility(View.GONE);
            } else {
                rb0.setVisibility(View.VISIBLE);
                rb4.setVisibility(View.VISIBLE);
                rb5.setVisibility(View.VISIBLE);
            }
            loadNotice_Config();
            //  toast("显示了");
            if (SPUtils.getInstance(mActivity).getBoolean("switch_message_only", false)) {
                tv_left.setText(" 发送短信");
                tv_right.setText(" 完成订单");
            } else {
                tv_left.setText(" 仅完成订单");
                tv_right.setText(" 完成并发短信");
            }
        }
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_homepage;
    }

    @Override
    protected void findViews() {
        mView = getView();
        dialog = new ZLoadingDialog(mActivity);
        dialog.setLoadingBuilder(Z_TYPE.DOUBLE_CIRCLE)//设置类型
//                .setLoadingColor(Color.RED)//颜色
                .setHintText("加载中...")
//                .setHintTextSize(16) // 设置字体大小 dp
                .setHintTextColor(Color.parseColor("#CCCCCC"))  // 设置字体颜色
//                .setDurationTime(0.5) // 设置动画时间百分比 - 0.5倍
                .setDialogBackgroundColor(Color.parseColor("#00000000")); // 设置背景色，默认白色
        netUtils = new NetUtils(mActivity, netRequestCallBack);
        img_scanner = getView(R.id.img_scanner);
        editText = getView(R.id.edittext_search);
        tv_shaixuan = getView(R.id.tv_shaixuan);
        radioGroup = getView(R.id.rd_group);
        rb0 = getView(R.id.rb_havepay);
        rb1 = getView(R.id.rb_all);
        rb2 = getView(R.id.rb_timetable);
        rb3 = getView(R.id.rb_prepare);
        rb4 = getView(R.id.rb_review);
        rb44 = getView(R.id.rb_checked);
        rb5 = getView(R.id.rb_hasda);
        swipeRefreshLayout = getView(R.id.swipe_refres);
        recyclerView = getView(R.id.list_rcy);
        rlayout_xiugai = getView(R.id.rlayout_xiugai);
        tv_jiedan = getView(R.id.tv_jiedan);
        checkbox_all = getView(R.id.checkbox_all);
        btn_suresend = getView(R.id.btn_suresend);

        layout_btn = getView(R.id.layout_btn);
        tv_left = getView(R.id.tv_btn_left);
        tv_right = getView(R.id.tv_btn_right);
//        textFragment = getView(R.id.text_fragment);

//        danmakuView = getView(R.id.danmaku_view);
//        danmakuView.enableDanmakuDrawingCache(true);
//        danmakuView.setCallback(new DrawHandler.Callback() {
//            @Override
//            public void prepared() {
//                showDanmaku = true;
//                danmakuView.start();
//                generateSomeDanmaku();
//               // addDanmaku("下雨天，同学骑士请穿好雨衣，不要着凉哦-。-", false);
//            }
//
//            @Override
//            public void updateTimer(DanmakuTimer timer) {
//            }
//
//            @Override
//            public void danmakuShown(BaseDanmaku danmaku) {
//            }
//
//            @Override
//            public void drawingFinished() {
//            }
//        });
//        danmakuContext = DanmakuContext.create();
//        danmakuView.prepare(parser, danmakuContext);

    }

    @Override
    protected void formatViews() {
//        textFragment.setText("找到了，而且赋值成功");
        img_scanner.setOnClickListener(this);
        tv_shaixuan.setOnClickListener(this);
        btn_suresend.setOnClickListener(this);
        editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if ((actionId == 0 || actionId == 3) && event != null) {
                    //搜索
                    loaddata();
                }
                return false;
            }
        });
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rb_havepay:
                        //已支付
                        status = "paid";
                        rlayout_xiugai.setVisibility(View.GONE);
                        btn_suresend.setVisibility(View.VISIBLE);
                        layout_btn.setVisibility(View.GONE);
                        break;
                    case R.id.rb_all:
                        //已接单
                        status = "received_order";
                        btn_suresend.setText("确认送出");
                        rlayout_xiugai.setVisibility(View.GONE);
                        btn_suresend.setVisibility(View.VISIBLE);
                        layout_btn.setVisibility(View.GONE);
                        break;
                    case R.id.rb_timetable:
                        //已取餐 take_food
                        status = "take_food";
                        btn_suresend.setText("到达中转");
                        rlayout_xiugai.setVisibility(View.GONE);
                        btn_suresend.setVisibility(View.VISIBLE);
                        layout_btn.setVisibility(View.GONE);
                        break;
                    case R.id.rb_prepare:
                        //中转
                        status = "transfer";
                        btn_suresend.setText("确认取餐");
                        rlayout_xiugai.setVisibility(View.GONE);
                        btn_suresend.setVisibility(View.VISIBLE);
                        layout_btn.setVisibility(View.GONE);
                        break;
                    case R.id.rb_review:
                        //配送中
                        status = "distributing";
                        btn_suresend.setText("确认送达");
                        rlayout_xiugai.setVisibility(View.GONE);
                        //msg_type 	Int 短信通知 0:未选择， 1： 勾选， 2: 不勾选
                        if (Msg_type == 1) {
                            btn_suresend.setVisibility(View.VISIBLE);
                            btn_suresend.setText("完成并发送短信");
                            layout_btn.setVisibility(View.GONE);
                            send_msg = 1;
                        } else {
                            btn_suresend.setVisibility(View.GONE);
                            layout_btn.setVisibility(View.VISIBLE);
                            send_msg = 0;
                        }
                        if (SPUtils.getInstance(mActivity).getBoolean("switch_message_only", false)) {
                            btn_suresend.setVisibility(View.GONE);
                            layout_btn.setVisibility(View.VISIBLE);
                            tv_left.setText(" 发送短信");
                            tv_right.setText(" 完成订单");
                        }
                        break;
                    case R.id.rb_checked:
                        //已入柜
                        status = "sark";
                        btn_suresend.setText("确认送达");
                        rlayout_xiugai.setVisibility(View.GONE);
                        btn_suresend.setVisibility(View.VISIBLE);
                        layout_btn.setVisibility(View.GONE);
                        break;
                    case R.id.rb_hasda:
                        //已送达
                        status = "delivery";
                        rlayout_xiugai.setVisibility(View.GONE);
                        btn_suresend.setVisibility(View.VISIBLE);
                        layout_btn.setVisibility(View.GONE);
                        break;
                }

                dialog.show();
                // LoadingDialog.getInstance(mActivity.getParent()).show();
                page = 0;
                loaddata();
            }
        });
        //全选按钮
        checkbox_all.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (compoundButton.isChecked()) {
                    for (OrderListBean bean : list) {
                        bean.isSelect = true;
                    }
                }
//                else {
//                    for (OrderListBean bean : list) {
//                        bean.isSelect = false;
//                    }
//                }
                adapter.notifyDataSetChanged();
            }
        });
        tv_left.setOnClickListener(this);
        tv_right.setOnClickListener(this);
    }

    @Override
    protected void formatData() {
        LinearLayoutManager layoutManager = new LinearLayoutManager(mActivity);
        layoutManager.setOrientation(LinearLayout.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);
        adapter = new OrderListAdapter(R.layout.item_order, list);
        adapter.bindToRecyclerView(recyclerView);
        recyclerView.setAdapter(adapter);
        adapter.setEmptyView(R.layout.empty_view);
        //下拉刷新
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                // lists.clear();
                page = 0;
                loaddata();
                rlayout_xiugai.setVisibility(View.GONE);
            }
        });
        //上拉加载
        adapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                recyclerView.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        loaddata();
                    }
                }, 500);
                rlayout_xiugai.setVisibility(View.GONE);
            }
        });
        adapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, final int position) {
                switch (view.getId()) {
                    case R.id.img_select:
                        list.get(position).isSelect = !list.get(position).isSelect;
                        adapter.notifyDataSetChanged();
                        int i = 0;
                        for (OrderListBean bean : list) {
                            if (bean.isSelect) {
                                i++;
                            }
                        }
                        if (i > 0) {
                            if (status.equals("take_food") || status.equals("distributing")) {
                                checkbox_all.setVisibility(View.VISIBLE);
                            } else {
                                checkbox_all.setVisibility(View.GONE);
                            }
                            if (i == list.size()) {
                                checkbox_all.setChecked(true);
                            } else {
                                checkbox_all.setChecked(false);
                            }
                            rlayout_xiugai.setVisibility(View.VISIBLE);
                            tv_jiedan.setText("已选" + i + "单");
                            if (SPUtils.getInstance(mActivity).getString("Is_coach").equals("2")) {
                                btn_suresend.setText("分配骑手");
                            }
                        } else {
                            rlayout_xiugai.setVisibility(View.GONE);
                        }

                        break;
                    case R.id.img_phone:
//                        if (TextUtils.isEmpty(list.get(position).getAddr_phone())) {
//                            toast("联系方式不能为空,请联系客服");
//                            return;
//                        }
                        CallPhoneNumber(list.get(position).getOrder_no());
//                        callPhone(list.get(position).getAddr_phone());
                        break;
                    case R.id.tv_time_name:
                        new HiDialog.Builder(getActivity())
                                .setTitle("配送超时说明")
                                .setContent("预定单标准送达时间为顾客期望时间，提前20分钟送达或延迟送达均判定为超时，视情况给予奖励金!")
                                .setRightBtnText("我知道了")
                                .build();
                        break;
                    case R.id.img_is_sure:
                        mposition = position;
//                        list.get(position).isSure = !list.get(position).isSure;
//                        adapter.notifyDataSetChanged();
                        //标记是否已取餐
                        Map<String, Object> map = new HashMap();
                        map.put("order_no", "" + list.get(position).getOrder_no());
                        if (list.get(position).getTake_tag().equals("1")) {
                            map.put("take_tag", "2");
                        } else {
                            map.put("take_tag", "1");
                        }
                        netUtils.post("rider/user/set-tag", map, BaseBean.class, false);
                        break;
                    case R.id.img_daodian:
                        list.get(position).DaoDian = true;
                        adapter.notifyDataSetChanged();

                        //6.0权限处理
                        Acp.getInstance(mActivity).request(new AcpOptions.Builder().setPermissions(
                                Manifest.permission.ACCESS_FINE_LOCATION).setPermissions(Manifest.permission.ACCESS_COARSE_LOCATION).build(), new AcpListener() {
                            @SuppressLint("MissingPermission")
                            @Override
                            public void onGranted() {
                                if (!isOPen(mContext)) {
                                    openGPS(mContext);
                                    toast("请打开GPS定位！");
                                } else {
                                    //标记是否已到店
                                    Map<String, Object> mapd = new HashMap();
                                    mapd.put("order_no", "" + list.get(position).getOrder_no());
                                    mapd.put("long", "" + SPUtils.getInstance(mContext).getString("longitude"));
                                    mapd.put("lat", "" + SPUtils.getInstance(mContext).getString("latitude"));
                                    netUtils.post("rider/user/out-arrive-order", mapd, BaseBean.class, false);
                                }
                            }

                            @Override
                            public void onDenied(List<String> permissions) {
                                toast("请开启定位权限，否则无法确认到店！");
                            }
                        });

                        break;
                }
            }
        });
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Bundle bundle = new Bundle();
                bundle.putString("order_no", list.get(position).getOrder_no());
                bundle.putInt("end_time", list.get(position).getEnd_time());
                bundle.putString("status", status);
                jumpTo(OrderDetailActivity.class, bundle);
            }
        });
        loaddata();
    }

    private void loaddata() {
        if (TextUtils.isEmpty(editText.getText().toString())) {
            Map<String, Object> map = new HashMap<>();
            map.put("addr_id", "" + addr_id);
            map.put("canteen_id","" + can_id);
            if (TextUtils.isEmpty(type_id)) {
                map.put("distribution_type", "0");
            } else {
                map.put("distribution_type", type_id);
            }
            map.put("status", "" + status);
            map.put("page", "" + page);
            map.put("limit", "10");
            netUtils.get("rider/user/order-list", map, BaseBean.class, null, false);
        } else {
            Map<String, Object> map = new HashMap<>();
            map.put("status", "" + status);
            map.put("keyword", "" + editText.getText());
            netUtils.get("rider/user/search", map, BaseBean.class, null, false);
        }

    }

    private void CallPhoneNumber(String order_no){
        Map<String, Object> map = new HashMap<>();
        map.put("order_no", order_no);
        map.put("type", "1"); //1:用户电话， 2:用户备用电话
        netUtils.postNew("notice/order-call", map, BaseBean.class, false);
    }

    @Override
    protected void getBundle(Bundle bundle) {

    }

    private void loadtake_order(String order_nos) {
        Map<String, Object> map = new HashMap<>();
        if (TextUtils.isEmpty(order_nos)) {
            return;
        }
        order_nos = order_nos.substring(0, order_nos.length() - 1);
        map.put("order_nos", "" + order_nos);
        dialog.show();
        //LoadingDialog.getInstance(mActivity.getParent()).show();
        netUtils.post("rider/user/take-order", map, BaseBean.class, false);
    }

    private void loadfinish_dispatch(String order_nos) {
        Map<String, Object> map = new HashMap<>();
        if (TextUtils.isEmpty(order_nos)) {
            return;
        }
        order_nos = order_nos.substring(0, order_nos.length() - 1);
        map.put("order_nos", "" + order_nos);
        // send_msg 的参数，0:不发，1:发送
        map.put("send_msg", "" + send_msg);
        dialog.show();
        //LoadingDialog.getInstance(mActivity.getParent()).show();
        netUtils.post("rider/user/finish-dispatch", map, BaseBean.class, false);
    }

    /**
     * 仅发送短信
     *
     * @param order_nos
     */
    private void OnlySendMsg(String order_nos) {
        Map<String, Object> map = new HashMap<>();
        if (TextUtils.isEmpty(order_nos)) {
            return;
        }
        order_nos = order_nos.substring(0, order_nos.length() - 1);
        map.put("order_nos", "" + order_nos);
        map.put("type", 1); // 1 短信 ，2 电话
        dialog.show();
        //LoadingDialog.getInstance(mActivity.getParent()).show();
        netUtils.post("rider/user/send-msg", map, BaseBean.class, false);
    }

    /**
     * 配送区域 - 通知设置
     */
    private void loadNotice_Config() {
        Map<String, Object> map = new HashMap<>();
        netUtils.post("rider/user/notice-config", map, BaseBean.class, false);
    }

    /**
     * 打开相机，进入扫描二维码
     */
    @SuppressLint("MissingPermission")
    public void ScanCode() {
        //6.0权限处理
        Acp.getInstance(mActivity).request(new AcpOptions.Builder().setPermissions(
                Manifest.permission.CAMERA).build(), new AcpListener() {
            @SuppressLint("MissingPermission")
            @Override
            public void onGranted() {
                jumpTo(CaptureActivity.class, 0);
            }

            @Override
            public void onDenied(List<String> permissions) {
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.img_scanner:
                ScanCode();
                //jumpTo(CaptureActivity.class);
                break;
            case R.id.tv_shaixuan:
                //弹出筛选
                if (pop == null) {
                    pop = new SieveHomePagePopupWindow(getActivity(), new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            if (view.getId() == R.id.btn_reset) {
                                addr_id = "";
                                type_id = "";
                                pop.resetAddr_Id();
                            } else if (view.getId() == R.id.btn_sure) {
                                addr_id = pop.ReturnAddr_Id();
                                type_id = pop.ReturnType_Id();
                                can_id = pop.ReturnCan_Id();
                                pop.dismiss();
                                dialog.show();
                                page = 0;
                                loaddata();
                            }
                            SPUtils.getInstance(mContext).put("can_id",type_id);
                            SPUtils.getInstance(mContext).put("type_id",type_id);
                            SPUtils.getInstance(mContext).put("addr_id",addr_id);
                        }
                    });
                    pop.showAsDropDown(editText);
                } else {
                    pop.showAsDropDown(editText);
                }
                break;
            case R.id.btn_suresend:
                String order_no = "";
                int j = 0;
                for (int i = 0; i < list.size(); i++) {
                    if (list.get(i).isSelect) {
                        j++;
                        order_no += list.get(i).getOrder_no() + ",";
                    }
                }
                //修改订单状态
                if (btn_suresend.getText().toString().equals("确认送出")) {
                    loadtake_order(order_no);
                } else if (btn_suresend.getText().toString().equals("到达中转")) {
                    loadfinish_dispatch(order_no);
                } else if (btn_suresend.getText().toString().equals("确认取餐")) {
                    loadtake_order(order_no);
                } else if (btn_suresend.getText().toString().equals("确认送达")) {
                    loadfinish_dispatch(order_no);
                } else if (btn_suresend.getText().toString().equals("完成并发送短信")) {
                    loadfinish_dispatch(order_no);
                } else if (btn_suresend.getText().toString().equals("分配骑手")) {
                    //跳转至分配骑手
                    if (TextUtils.isEmpty(order_no)) {
                        return;
                    }
                    if (TextUtils.isEmpty(addr_id) || addr_id.contains(",")) {
                        toast("调度员请先点击筛选，选择单个区域后分配骑手");
                        return;
                    }
                    order_no = order_no.substring(0, order_no.length() - 1);
                    Bundle bundle = new Bundle();
                    bundle.putString("order_no", order_no);
                    bundle.putString("addr_id", addr_id + "");
                    bundle.putString("order_num", j + "");
                    bundle.putString("homepage", "true");
                    jumpTo(DispatchRiderActivity.class, 0, bundle);
                }
                rlayout_xiugai.setVisibility(View.GONE);
                for (OrderListBean bean : list) {
                    bean.isSelect = false;
                }
                adapter.notifyDataSetChanged();
                break;
            case R.id.tv_btn_left:
                if (tv_left.getText().toString().contains("发送短信")) {
                    String horder_no = "";
                    for (int i = 0; i < list.size(); i++) {
                        if (list.get(i).isSelect) {
                            horder_no += list.get(i).getOrder_no() + ",";
                        }
                    }
                    OnlySendMsg(horder_no);
                } else {
                    //TODO 仅完成订单
                    send_msg = 0;
                    String horder_no = "";
                    for (int i = 0; i < list.size(); i++) {
                        if (list.get(i).isSelect) {
                            horder_no += list.get(i).getOrder_no() + ",";
                        }
                    }
                    loadfinish_dispatch(horder_no);
                }
                rlayout_xiugai.setVisibility(View.GONE);
                break;
            case R.id.tv_btn_right:
                if (tv_right.getText().toString().contains("完成订单")) {
                    send_msg = 0;
                    String lorder_no = "";
                    for (int i = 0; i < list.size(); i++) {
                        if (list.get(i).isSelect) {
                            lorder_no += list.get(i).getOrder_no() + ",";
                        }
                    }
                    loadfinish_dispatch(lorder_no);
                } else {
                    //TODO 完成订单并发送短信
                    send_msg = 1;
                    String lorder_no = "";
                    for (int i = 0; i < list.size(); i++) {
                        if (list.get(i).isSelect) {
                            lorder_no += list.get(i).getOrder_no() + ",";
                        }
                    }
                    loadfinish_dispatch(lorder_no);
                }
                rlayout_xiugai.setVisibility(View.GONE);
                break;
        }
    }

    private NetUtils.NetRequestCallBack netRequestCallBack = new NetUtils.NetRequestCallBack() {
        @Override
        public void success(String action, BaseBean t, Map tag) {
            dialog.dismiss();
            //LoadingDialog.getInstance(mActivity.getParent()).dismiss();
            swipeRefreshLayout.setRefreshing(false);

            if (action.equals("rider/user/search")) {
                if (t.getCode() != 200) {
                    toast(t.getMsg());
                } else {
                    list.clear();
                    list = GsonUtils.getInstance(mActivity).parseNoHeaderJArray(t, OrderListBean.class);
                    if (status.equals("delivery")) {
                        for (OrderListBean bean : list) {
                            bean.isGone = true;
                        }
                    }
                    adapter.setNewData(list);
                    adapter.loadMoreEnd();
                }
            } else if (action.equals("rider/user/take-order")) {
                if (t.getCode() != 200) {
                    toast(t.getMsg());
                } else {
                    page = 0;
                    loaddata();
                }
            } else if (action.equals("rider/user/finish-dispatch")) {
                if (t.getCode() != 200) {
                    toast(t.getMsg());
                } else {
                    page = 0;
                    loaddata();
                }
            } else if (action.equals("rider/user/set-tag")) {
                if (t.getCode() != 200) {
                    toast(t.getMsg());
                } else {
                    if (list.get(mposition).getTake_tag().equals("1")) {
                        list.get(mposition).setTake_tag("2");
                    } else {
                        list.get(mposition).setTake_tag("1");
                    }
                    adapter.notifyDataSetChanged();
                }
            } else if (action.equals("rider/user/out-arrive-order")) {
                if (t.getCode() != 200) {
                    toast(t.getMsg());
                } else {
                    list.get(mposition).out_arrive_time = 1;
                }
            } else if (action.equals("rider/user/notice-config")) {
                if (t.getCode() != 200) {
                    toast(t.getMsg());
                } else {
                    NoticeConfigBean bean = (NoticeConfigBean) GsonUtils.getInstance(mActivity).parseComplexJArrayByCommon(t, NoticeConfigBean.class);
                    Msg_type = bean.getMsg_type();
                }
            }else if(action.equals("rider/user/send-msg")){
                if (t.getCode() != 200) {
                    toast(t.getMsg());
                } else {
                    toast("发送短信成功");
                    page = 0;
                    loaddata();
                }
            }else if(action.equals("notice/order-call")){
                if (t.ok != 1) {
                    toast(t.getMsg());
                } else {
                    callPhone(t.getCall_number());
                }
            } else {
                if (t.getCode() == 200) {
                    if (page == 0) {
                        list.clear();
                        OrderListMoreBean bean = (OrderListMoreBean) GsonUtils.getInstance(mActivity).parseComplexJArrayByCommon(t, OrderListMoreBean.class);
                        //list = bean.getList();
                        ShaiXuan(bean.getList());
                        initData(bean);
                        adapter.setNewData(list);
                        page = 1;
                    } else {
                        if (list.size() >= t.getCount()) {
                            adapter.loadMoreEnd();
                        } else {
                            OrderListMoreBean bean = (OrderListMoreBean) GsonUtils.getInstance(mActivity).parseComplexJArrayByCommon(t, OrderListMoreBean.class);
                            //list.addAll(bean.getList());
                            ShaiXuan(bean.getList());
                            initData(bean);
                            adapter.notifyDataSetChanged();
                            page++;
                            adapter.loadMoreComplete();
                        }
                    }
                } else {
                    toast(t.getMsg());
                }
            }

        }

        @Override
        public void error(String action, Throwable e, Map tag) {
            dialog.dismiss();
            swipeRefreshLayout.setRefreshing(false);
            adapter.loadMoreComplete();

        }
    };

    private void initData(OrderListMoreBean bean) {
        rb0.setText("已支付(" + bean.getNums().getPaid() + ")");
        rb1.setText("已接单(" + bean.getNums().getReceived_order() + ")");
        rb2.setText("已取餐(" + bean.getNums().getTake_food() + ")");
        rb3.setText("中转(" + bean.getNums().getTransfer() + ")");
        rb4.setText("配送中(" + bean.getNums().getDistributing() + ")");
        rb44.setText("已入柜(" + bean.getNums().getSark() + ")");
        rb5.setText("已送达(" + bean.getNums().getDelivery() + ")");
        for (OrderListBean sbean : list) {
            sbean.time_lag = bean.getTime_lag();
            sbean.status = status;
        }

        if (status.equals("delivery") || status.equals("paid")) {
            for (OrderListBean sbean : list) {
                sbean.isGone = true;
            }
        }
        if (!status.equals("distributing")) {
            for (OrderListBean sbean : list) {
                sbean.isSureGone = true;
            }
        }
        if (!status.equals("received_order")) {
            for (OrderListBean sbean : list) {
                sbean.isDaoDianGone = true;
            }
        }
        if (SPUtils.getInstance(mActivity).getString("Is_coach").equals("2")) {
            if (status.equals("take_food") || status.equals("distributing") || status.equals("sark")) {
                for (OrderListBean sbean : list) {
                    sbean.isGone = true;
                }
            }
        } else if (SPUtils.getInstance(mActivity).getString("Is_coach").equals("1")) {
            if (status.equals("take_food") || status.equals("sark")) {
                for (OrderListBean sbean : list) {
                    sbean.isGone = true;
                }
            }
        } else if (SPUtils.getInstance(mActivity).getString("Is_coach").equals("3")) {
            if (status.equals("distributing") || status.equals("transfer") || status.equals("sark")) {
                for (OrderListBean sbean : list) {
                    sbean.isGone = true;
                }
            }
        }
    }

    /**
     * 筛选 校内校外，仅校内 ，订单
     */
    private void ShaiXuan(List<OrderListBean> listBeans) {
        if (!TextUtils.isEmpty(type_id)) {
            String[] str = type_id.split(",");
            for (OrderListBean listBean : listBeans) {
                if (str.length > 1) {
                    if (listBean.getDistribution_type() == Integer.parseInt(str[0]) || listBean.getDistribution_type() == Integer.parseInt(str[1])) {
                        list.add(listBean);
                    }
                } else {
                    if (listBean.getDistribution_type() == Integer.parseInt(str[0])) {
                        list.add(listBean);
                    }
                }

            }
            //list = mlistBeans;
        } else {
            list.addAll(listBeans);
        }
    }


    /**
     * 拨打电话（直接拨打电话）
     *
     * @param phoneNumber 电话号码
     */
    @SuppressLint("MissingPermission")
    public void callPhone(final String phoneNumber) {
        if (mActivity == null || android.text.TextUtils.isEmpty(phoneNumber)) {
            return;
        }
        if (phoneNumber.length() < 8 || phoneNumber.length() > 11) {
            toast("电话号码格式或输入有误");
            return;
        }
        //6.0权限处理
        Acp.getInstance(mActivity).request(new AcpOptions.Builder().setPermissions(
                Manifest.permission.CALL_PHONE).build(), new AcpListener() {
            @SuppressLint("MissingPermission")
            @Override
            public void onGranted() {
                Uri u = Uri.parse("tel:" + phoneNumber);
                Intent it = new Intent(Intent.ACTION_CALL, u);
                mActivity.startActivity(it);
            }

            @Override
            public void onDenied(List<String> permissions) {
            }
        });
    }

    /**
     * 判断GPS是否开启，GPS或者AGPS开启一个就认为是开启的
     *
     * @param context
     * @return true 表示开启
     */
    public static final boolean isOPen(final Context context) {
        LocationManager locationManager
                = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        // 通过GPS卫星定位，定位级别可以精确到街（通过24颗卫星定位，在室外和空旷的地方定位准确、速度快）
        boolean gps = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
        // 通过WLAN或移动网络(3G/2G)确定的位置（也称作AGPS，辅助GPS定位。主要用于在室内或遮盖物（建筑群或茂密的深林等）密集的地方定位）
        boolean network = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
        if (gps || network) {
            return true;
        }

        return false;
    }

    /**
     * 强制帮用户打开GPS
     *
     * @param context
     */
    public static final void openGPS(Context context) {
        Intent GPSIntent = new Intent();
        GPSIntent.setClassName("com.android.settings",
                "com.android.settings.widget.SettingsAppWidgetProvider");
        GPSIntent.addCategory("android.intent.category.ALTERNATIVE");
        GPSIntent.setData(Uri.parse("custom:3"));
        try {
            PendingIntent.getBroadcast(context, 0, GPSIntent, 0).send();
        } catch (PendingIntent.CanceledException e) {
            e.printStackTrace();
        }
    }
//    @Override
//    public void onResume() {
//        super.onResume();
//        if (SPUtils.getInstance(mActivity).getString("Is_coach").equals("2")) {
//            tv_shaixuan.setVisibility(View.GONE);
//        } else {
//            tv_shaixuan.setVisibility(View.VISIBLE);
//        }
//    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == 100) {
            Logger.d("ActivityResult--->", "ActivityResult");
            page = 0;
            loaddata();
        }
    }

//    /**
//     * 向弹幕View中添加一条弹幕
//     *
//     * @param content    弹幕的具体内容
//     * @param withBorder 弹幕是否有边框
//     */
//    private void addDanmaku(String content, boolean withBorder) {
//        BaseDanmaku danmaku = danmakuContext.mDanmakuFactory.createDanmaku(BaseDanmaku.TYPE_SCROLL_RL);
//        danmaku.text = content;
//        danmaku.padding = 5;
//        danmaku.textSize = DensityUtil.sp2px(mContext, 15);
//        danmaku.textColor = Color.WHITE;
//        danmaku.setTime(danmakuView.getCurrentTime());
//        if (withBorder) {
//            danmaku.borderColor = Color.GREEN;
//        }
//        danmakuView.addDanmaku(danmaku);
//    }
//
//    /**
//     * 随机生成一些弹幕内容以供测试
//     */
//    private void generateSomeDanmaku() {
//        new Thread(new Runnable() {
//            @Override
//            public void run() {
//                while (showDanmaku) {
//                    int time = new Random().nextInt(30000);
//                    String content = "下雨天，同学骑士请穿好雨衣，不要着凉哦-。-" + time + time;
//                    addDanmaku(content, false);
//                    try {
//                        Thread.sleep(time);
//                    } catch (InterruptedException e) {
//                        e.printStackTrace();
//                    }
//                }
//            }
//        }).start();
//    }

}
